package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import com.sky.service.impl.ShoppingCartServiceImpl;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController()
@RequestMapping("/user/shoppingCart")
@Slf4j
@CrossOrigin
public class ShoppingCartController {


    @Autowired
    private ShoppingCartService shoppingCartService;


    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> list(){
        log.info("查看购物车:{}",BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartService.listAll();

        return Result.success(list);
    }


    @PostMapping("/add")
    @ApiOperation("添加到购物车")

    public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加到购物车:{}",shoppingCartDTO);

        shoppingCartService.add(shoppingCartDTO);

        return Result.success();
    }

    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result<String> clean(){
        log.info("清空购物车：{}", BaseContext.getCurrentId());

        List<Long> ids=new ArrayList<>();
        ids.add(BaseContext.getCurrentId());
        shoppingCartService.deleteBatch(ids);

        return Result.success();
    }
    @PostMapping("/sub")
    @ApiOperation("删除购物车中的一个商品")
    public Result<String> sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车中的一个商品:{}",shoppingCartDTO);

        shoppingCartService.sub(shoppingCartDTO);
        return Result.success();
    }


}
